<?php
	class Default_Model_Product extends Zend_Db_Table_Abstract
	{
		protected $_name = 'product';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}

        public function getItemByCategory($category_id, $limit = null)
        {
            $data = array(
                'id',
                'name',
                'image',
                'thumbnail',
                'price',
                'price_vat',
                'size',
                'url',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('category_id = ?', $category_id)
                            ->where('status = 1')
                            ->order('id DESC');
            if($limit != null) {
                $sql = $sql->limit($limit);
                $result = $this->_db->fetchAll($sql);
                return $result;
            }
            else {
                return $sql;
            }
        }
        
        public function getItem($id)
        {
            $product = array(
                'p.name',
                'p.code',
                'p.image',
                'p.price',
                'p.price_vat',
                'p.size',
                'p.description',
                'p.category_id',
            );
            $provider = array(
                'pv.name AS provider_name',
            );
            $sql = $this->_db->select()
                            ->from('product AS p', $product)
                            ->joinLeft('provider AS pv', 'p.provider_id = pv.id', $provider)
                            ->where('p.id = ?', $id)
                            ->where('p.status = 1');
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getItemConnect($category_id, $id)
        {
            $data = array(
                'id',
                'name',
                'image',
                'thumbnail',
                'price',
                'price_vat',
                'size',
                'url',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('category_id = ?', $category_id)
                            ->where('id <> ?', $id)
                            ->where('status = 1')
                            ->order('id DESC')
                            ->limit(6);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function listImage()
        {
            $sql = $this->_db->select()
                            ->from($this->_name, array('image'))
                            ->where('status = 1')
                            ->order('id DESC')
                            ->limit(4);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
    }
